<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="Profileobj.photo"
            @click="iputopen"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="Profileobj.name"
        @click="editName"
      />
      <van-cell
        title="生日"
        is-link
        :value="Profileobj.birthday"
        @click="changBrith"
      />
    </van-cell-group>

    <!-- //修改名字 -->
    <van-dialog
      v-model="show"
      title="修改名称"
      show-cancel-button
      :before-close="closeFn"
    >
      <input type="text" v-fofo placeholder="请输入用户名" v-model="userName" />
    </van-dialog>

    <!-- 修改时间 -->
    <van-popup
      v-model="popshow"
      round
      position="bottom"
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmFn"
        @cancel="popshow = false"
      />
    </van-popup>
  </div>
</template>

<script>
import { UserProfileAPI, UpdataUserphotoAPI, updateProfileAPI } from '@/api'
import { Notify } from 'vant'
import moment from 'moment'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      Profileobj: {},
      show: false,
      userName: '',
      popshow: false,
      // 时间需要
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  async created () {
    const res = await UserProfileAPI()
    this.Profileobj = res.data.data
  },
  methods: {
    ...mapMutations(['updatePhoto', 'updateName']),
    // --------------------------------------------------------上传图片
    iputopen () {
      // 点击打开input
      this.$refs.iptFile.click()
    },
    async onFileChange (ev) {
      // 创建一个formdata对象
      const fd = new FormData()
      // 在fordata对象中插入
      fd.append('photo', ev.target.files[0])
      // 发送请求传入fd(fordata)对象
      const res = await UpdataUserphotoAPI(fd)
      // 把接口传回来的photo值赋值
      this.Profileobj.photo = res.data.data.photo
      this.updatePhoto(res.data.data.photo)
    },
    // --------------------------------------------------------修改用户姓名
    editName () {
      this.show = true
      this.userName = this.Profileobj.name
    },
    // --------------------------------------------------------修改用户姓名并验证
    async closeFn (action, done) {
      if (action === 'confirm') {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          await updateProfileAPI({
            userName: this.userName
          })
          this.Profileobj.name = this.userName
          this.updateName(this.userName)
          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        // 取消
        done()
      }
    },
    // --------------------------------------------------------修改生日
    changBrith () {
      this.popshow = true
      this.currentDate = new Date(this.Profileobj.birthday)
    },
    // --------------------------------------------------------确定修改生日
    async confirmFn () {
      const dateStr = moment(this.currentDate).format('YYYY-MM-DD')
      //   console.log(dateStr)
      await updateProfileAPI({
        birthday: dateStr
      })
      // 前端页面同步
      //   console.log(this.Profileobj)
      this.Profileobj.birthday = dateStr
      // 时间选择器关闭
      this.popshow = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  /* 对input标签居中 */
  text-align: center;
  input {
    border: none;
    /* inupt内容居中 */
    text-align: center;
    margin: 15px 0 10px 0;
    color: #999;
    font-size: 14px;
  }
}
</style>
